<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');

$aCamForm = array();
require("_inc/const.campiUorg.php");

if (reqVar('azione') == 'salva') {
	$msgPag = conferma($aCamForm);
} 

$sqlSel = "SELECT u.* FROM rgl_uorg u WHERE u.id_uorg = ".reqVarInt('id');
$objDB->exec($sqlSel);
if(!$aRow = $objDB->fetch()) {
	backOnErr("id. errato");
}
reqDef('old_cod_uorg',$aRow['cod_uorg']);
reqDef('cod_uorg',$aRow['cod_uorg']);
reqDef('descr',$aRow['descr']);
reqDef('id_uorg_padre',$aRow['id_uorg_padre']);

$sqlSel = "SELECT u.*,uuo.id_uorg FROM rgl_utenti u "
	." LEFT JOIN rgl_utenti_uorg uuo ON uuo.id_utente = u.id_utente "
	."    AND uuo.id_uorg = ".reqVarInt('id')
	." ORDER BY u.cod_utente";
$objDB->exec($sqlSel);
while($aRow = $objDB->fetch()) {
	reqDef('cbx_ut_'.$aRow['id_utente'],($aRow['id_uorg'])?'S':'');
	reqDef('cod_ut_'.$aRow['id_utente'],$aRow['cod_utente']);
	reqDef('des_ut_'.$aRow['id_utente'], $aRow['cognome']." ".$aRow['nome']);
}

$sCmdR = "";
$sCmdR .= "<a class=\"cmd1\" href=\"javascript:document.frm1.submit();\" >salva</a>";
$sCmdR .= cmdTorna('torna');

testaHtml('Gestione applicazione',$msgPag,'gest');
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?=getHtmlForm('frm1','',array('enctype'=>true));?>
<?=getHtmlHidden('azione','salva');?>
<?=getHtmlHidden('id');?>
<?=getHtmlHidden('old_cod_uorg');?>
<?=getHtmlHidden('xsn',XSN);?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Unita' organizzativa</td>
   </tr>
<?
echo getHtmlCampiForm($aCamForm);
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="lab1" align="center" colspan='3'>Utenti</td>
   </tr>
<?php 
foreach($_REQUEST as $nX => $vX) {
	if (substr($nX,0,7)!='cbx_ut_') continue;
	$idX = substr($nX,7);
?>
   <tr>
      <td class="txt1" align="center" width='1%'><?php echo getHtmlCheckBox('cbx_ut_'.$idX, 'S');?></td>
      <td class="txt1" align="left" width='10%'>
      	<?php echo getHtmlHidden('cod_ut_'.$idX);echo reqVar('cod_ut_'.$idX)?>
      </td>
      <td class="txt1" align="left" width='89%'>
      	<?php echo getHtmlHidden('des_ut_'.$idX);echo reqVar('des_ut_'.$idX)?>
      </td>
   </tr>
<?php 
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();
function conferma($aCamForm) {
	global $objDB;
	$msg = '';
	$_REQUEST['cod_uorg'] = strtoupper(trim(reqVar('cod_uorg')));
	if (reqVar('cod_uorg')=='') {
		$msg .= "Codice obbligatorio<br>";
	}
	if (preg_match("/[^-_A-Z0-9]/",reqVar('cod_uorg'))) {
		$msg = "Codice '".reqVar('cod_uorg')."' puo' contenere solo lettere numeri '-' e '_'<br>";
	}
	if (trim(reqVar('descr'))=='') {
		$msg .= "Descrizione obbligatoria<br>";
	}
	$sqlSel = "SELECT id_uorg FROM rgl_uorg " .
			" WHERE cod_uorg = '".reqVarAS('cod_uorg')."'".
			" AND id_uorg <> ".reqVarInt('id');
	$objDB->exec($sqlSel,'insIst');
	$aRow = $objDB->fetch('insIst');
	if ($aRow) {
		$msg .= "Codice gia' esistente<br>";
	}
	if (reqVar('id_uorg_padre')>0) {
		$sqlSel = "SELECT id_uorg FROM rgl_uorg " .
				" WHERE id_uorg_padre = ".reqVarInt('id');
		$objDB->exec($sqlSel,'insIst');
		$aRow = $objDB->fetch('insIst');
		if ($aRow) {
			$msg .= "Unita' organizzativa a cui sono gia' associati sottolivelli<br>";
		}
	}
	if ($msg != '') {
		return $msg;
	}
	$sSqlIns = "UPDATE rgl_uorg SET ";
	foreach($aCamForm as $rX => $aRX) {
		foreach($aRX as $jX => $aCX) {
			if ($aCX[0]=='password') continue;
			$sSqlIns .= $aCX[0]." = '".reqVarAS($aCX[0])."', ";
		}
	}
	$sSqlIns = substr($sSqlIns,0,-2)." WHERE id_uorg = ".reqVarInt('id');
	$objDB->exec($sSqlIns,'insRec');
	$newId = $objDB->lastInsertId('insRec');
	
	$sSqlUpd = "DELETE FROM rgl_utenti_uorg WHERE id_uorg = ".reqVarInt('id');
	$objDB->exec($sSqlUpd);
	foreach($_REQUEST as $nX => $vX) {
		if (substr($nX,0,7)!='cbx_ut_') continue;
		if ($vX != 'S') continue;
		$idX = substr($nX,7);
		$sSqlUpd = "INSERT INTO rgl_utenti_uorg SET id_uorg = ".reqVarInt('id')
			.", id_utente = $idX";
		$objDB->exec($sSqlUpd);
	}

	header("Location: ".urlTorna("unita' organizzativa '".reqVar('cod_uorg')."' modificata"));
	exit();
}
?>